package com.lanou.dllo.baisibudejie.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.dllo.baisibudejie.R;
import com.lanou.dllo.baisibudejie.application.BaseApplication;
import com.lanou.dllo.baisibudejie.db.MySQLiteLoginUtil;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends Activity implements OnClickListener {

    private ImageView finishIv;
    private TextView reginstered_number, forget_psw, login_register_qqTv, register_sinaTv, register_tencentTv;
    private EditText phono_numberEt, pswEt;
    private Button loginBtn;
    private boolean isLogin;
    private boolean isExit;
    private MySQLiteLoginUtil loginUtil;
    private String login_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginUtil = new MySQLiteLoginUtil(this);
        initView();//页面初始化


    }


    /**
     * 页面初始化方法
     */

    private void initView() {

        finishIv = (ImageView) findViewById(R.id.finishIv);//返回按钮
        finishIv.setOnClickListener(this);
        reginstered_number = (TextView) findViewById(R.id.reginstered_number);//注册账号
        reginstered_number.setOnClickListener(this);
        phono_numberEt = (EditText) findViewById(R.id.phono_numberEt);//账号输入框
        pswEt = (EditText) findViewById(R.id.pswEt);//密码输入框
        loginBtn = (Button) findViewById(R.id.loginBtn);//登录按钮
        loginBtn.setOnClickListener(this);
        forget_psw = (TextView) findViewById(R.id.forget_psw);//忘记密码
        forget_psw.setOnClickListener(this);
        login_register_qqTv = (TextView) findViewById(R.id.login_register_qqTv);//QQ登录
        login_register_qqTv.setOnClickListener(this);
//        register_sinaTv = (TextView) findViewById(R.id.register_sinaTv);//微博登录
//        register_sinaTv.setOnClickListener(this);
//        register_tencentTv = (TextView) findViewById(R.id.register_tencentTv);//腾讯微博登录
//        register_tencentTv.setOnClickListener(this);
    }

    /**
     * 监听方法
     */

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finishIv://返回按钮
                finish();
                break;
            case R.id.reginstered_number://注册账号
                Intent intent_reginster = new Intent(this, ReginsteredActivity.class);
                startActivity(intent_reginster);
                finish();
                break;
            case R.id.loginBtn://登录按钮
                login();
                break;
            case R.id.forget_psw://忘记密码

                break;
            case R.id.login_register_qqTv://QQ登录
                qqLogin();

                break;

        }
    }

    //qq登录
    private void qqLogin() {
        Intent intent_qq = new Intent();
        intent_qq.putExtra("qqLogin", true);
        intent_qq.putExtra("isExit", false);
        setResult(200, intent_qq);
        BaseApplication.setIsLogin(true);
        finish();
    }

    //点击登录
    private void login() {
        //判断是否有账号密码
        if (phono_numberEt.getText().toString() != null && phono_numberEt.getText().toString().length() > 0 && pswEt.getText().toString() != null && pswEt.getText().toString().length() > 0) {
            String psw = loginUtil.queryByLoginName(phono_numberEt.getText().toString());
            if (pswEt.getText().toString().equals(psw)) {//在数据库中找到对应的密码进行匹配
                isLogin = true;
                isExit = false;
                login_name = phono_numberEt.getText().toString();
                BaseApplication.setIsLogin(true);
                Intent intent_login = new Intent();
                intent_login.putExtra("isLogin", isLogin);
                intent_login.putExtra("isExit", isExit);
                intent_login.putExtra("login_name", login_name);
                Log.i("lanou00",login_name+"");
                setResult(400, intent_login);
                finish();
            } else {
                Toast.makeText(this, "账号或密码错误", Toast.LENGTH_LONG).show();
            }
        }
    }
}


